﻿using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;

namespace Language.Intellisense.Signature
{
    internal class Parameter : IParameter
    {
        #region Private Data

        private ISignature _parent;
        private Stub.Function _function;
        private int _parameterIndex;
        private Span _locus;

        #endregion Private Data

        #region Private Properties

        private Stub.Property Property => _function.Parameters[ _parameterIndex ];

        #endregion Private Properties

        #region C-Tor

        public Parameter( ISignature parent, Stub.Function function, int parameterIndex )
        {
            _parent = parent;
            _function = function;
            _parameterIndex = parameterIndex;
            _locus = CalculateLocus( function, parameterIndex );
        }

        #endregion C-Tor

        #region Private Functions

        private static Span CalculateLocus( Stub.Function function, int parameterIndex )
        {
            int start = function.Name.Length + "( ".Length;

            for ( int i = 0; i < parameterIndex; ++i )
            {
                Stub.Property property = function.Parameters[ i ];

                start += property.Name.Length + ", ".Length;
            }

            return new Span( start, function.Parameters[ parameterIndex ].Name.Length );
        }

        #endregion Private Functions

        #region IParameter

        string IParameter.Documentation => Property.TypeDeclaration.Name;

        Span IParameter.Locus => _locus;

        string IParameter.Name => Property.Name;

        Span IParameter.PrettyPrintedLocus => new Span( 0, 4 );

        ISignature IParameter.Signature => _parent;

        #endregion IParameter
    }
}
